package edu.uni.dormitory.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.uni.dormitory.bean.LiveBed;
import edu.uni.dormitory.globalbean.BedVo;
import edu.uni.dormitory.mapper.LiveBedMapper;
import edu.uni.dormitory.service.BedService;
import edu.uni.example.config.ExampleConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈楚城
 * @create 2019.05.03
 * @function 服务
 */
@Service
public class BedServiceImpl implements BedService {
    @Autowired
    private LiveBedMapper liveBedMapper;
    @Autowired
    private ExampleConfig globalConfig;

    /**
     * 新增床位
     * @param liveBed
     * @return
     */
    @Override
    public boolean insert(LiveBed liveBed) {
        return liveBedMapper.insert(liveBed)>0 ? true:false;
    }

    /**
     * 删除床位
     * @param id
     * @return
     */
    @Override
    public boolean delete(long id) {
        return liveBedMapper.deleteByPrimaryKey(id)>0 ? true:false;
    }

    /**
     * 修改床位
     * @param liveBed
     * @return
     */
    @Override
    public boolean update(LiveBed liveBed) {
        return liveBedMapper.updateByPrimaryKeySelective(liveBed)>0 ? true:false;
    }

    /**
     * 根据宿舍id查询床位
     * @param liveRoomId
     * @return
     */
    @Override
    public List<BedVo> selectLiveBedByRoomId(long liveRoomId) {
        return liveBedMapper.selectLiveBedByRoomId(liveRoomId);
    }

    /**
     * 根据宿舍区id查询床位
     * @param areaId
     * @return
     */
    @Override
    public List<BedVo> selectLiveBedByAreaId(long areaId) {
        return liveBedMapper.selectLiveBedByAreaId(areaId);
    }

    /**
     * 分页查询宿舍区的空床位信息
     * @param pageNum
     * @param areaId
     * @return
     */
    @Override
    public PageInfo<BedVo> selectPageByAreaIdNo(Integer pageNum, long areaId) {
        PageHelper.startPage(pageNum, globalConfig.getPageSize());
        List<BedVo> list=liveBedMapper.selectLiveBedByAreaId(areaId);
        List<BedVo> list0=new ArrayList<BedVo>();
        List<BedVo> list1=new ArrayList<BedVo>();
        for(BedVo BedVoList :list){
            if(BedVoList.getStatus()==0){
                list0.add(BedVoList);
            }else
                list1.add(BedVoList);

        }
        if(list1!=null){
            return new PageInfo<>(list1);
        }
        else
        return null;
    }

    /**
     * 根据床位id获取床位信息
     * @param liveBedId
     * @return
     */
    @Override
    public BedVo selectByliveBedId(long liveBedId) {
        return liveBedMapper.selectByliveBedId(liveBedId);
    }

    /**
     * 分页查询宿舍区的有人床位信息
     * @param pageNum
     * @param areaId
     * @return
     */
    @Override
    public PageInfo<BedVo> selectPageByAreaIdYes(Integer pageNum, long areaId) {
        PageHelper.startPage(pageNum, globalConfig.getPageSize());
        List<BedVo> list=liveBedMapper.selectLiveBedByAreaId(areaId);
        List<BedVo> list0=new ArrayList<BedVo>();
        List<BedVo> list1=new ArrayList<BedVo>();
        for(BedVo BedVoList :list){
            if(BedVoList.getStatus()==0){
                list0.add(BedVoList);
            }else
                list1.add(BedVoList);

        }
        if(list0!=null){
            return new PageInfo<>(list0);
        }
        else
            return null;
    }

    @Override
    public LiveBed selectByPrimaryKey(Long id) {
        return liveBedMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据性别获取空床位
     * @param userSex
     * @param status
     * @return
     */
    @Override
    public List<LiveBed> selectBedNO(Byte userSex, Byte status) {
        return liveBedMapper.selectBedNO(userSex,status);
    }

    @Override
    public int selectbedNumber(long roomId) {
        return liveBedMapper.selectbedNumber(roomId);
    }

    @Override
    public int selectpersonNumber(long roomId) {
        return liveBedMapper.selectpersonNumber(roomId);
    }
}
